package baidu.com.matedemo.dlp;

import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

import com.baidu.duer.libcore.adapter.CommonAdapter;
import com.baidu.duer.libcore.adapter.ViewHolder;
import com.baidu.duer.libcore.util.ClickUtil;
import com.baidu.duer.libcore.util.ConsoleLogger;
import com.baidu.duer.smartmate.out.DuerDevice;
import com.baidu.duer.smartmate.proxy.IConnectionListener;

import java.util.ArrayList;
import java.util.List;

import baidu.com.matedemo.DemoApp;
import baidu.com.matedemo.DemoIntent;
import baidu.com.matedemo.R;
import baidu.com.matedemo.base.DemoApplication;
import baidu.com.matedemo.base.ListBaseFragment;

/**
 * Created by niufei01 on 2017/9/11.
 */

public class FunctionFragment extends ListBaseFragment implements AdapterView.OnItemClickListener, IConnectionListener {

    private List<ItemInfo> dataList = new ArrayList<ItemInfo>();
    CommonAdapter<ItemInfo> adapter = null;

    @Override
    public void onViewCreated(View view) {
        adapter = new CommonAdapter<ItemInfo>(getActivity(), R.layout.item_function, dataList) {
            @Override
            protected void convert(ViewHolder viewHolder, ItemInfo itemInfo, int i) {
                viewHolder.setText(R.id.text_view, itemInfo.getName());
            }
        };
        setAdapter(adapter);
        refreshData();
        DuerDevice currDevice = DemoApp.getInstance().getCurreDuerDevice();
        currDevice.setMaxReconnectTimes(3);
        currDevice.connect(getContext(), this);
    }

    public void refreshData() {
        dataList.clear();
        dataList.add(new ItemInfo(0, "设备id: " + DemoApp.getInstance().getCurreDuerDevice().getDeviceId()));
        dataList.add(new ItemInfo(1, "播放器"));
        dataList.add(new ItemInfo(2, "音量控制"));
        dataList.add(new ItemInfo(3, "设备信息"));
        dataList.add(new ItemInfo(4, "地理位置"));
        dataList.add(new ItemInfo(5, "闹钟提醒"));
        dataList.add(new ItemInfo(6, "协议支持信息"));
        dataList.add(new ItemInfo(7, "授权信息"));
        dataList.add(new ItemInfo(8, "电视互动"));
        dataList.add(new ItemInfo(9, "语音卡片上屏"));
        dataList.add(new ItemInfo(10, "音色设置"));
        dataList.add(new ItemInfo(11, "提醒功能"));
        dataList.add(new ItemInfo(12, "三方自定义协议"));
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (ClickUtil.isFastDoubleClick()) {
            return;
        }
        ItemInfo info = (ItemInfo) parent.getItemAtPosition(position);
        switch (info.getId()) {
            case 0:
                break;
            case 1:
                DemoIntent.intentToAudioPlayerDemo(getActivity(), info.getName());
                break;
            case 2:
                DemoIntent.intentToSpeakerDemo(getActivity(), info.getName());
                break;
            case 3:
                DemoIntent.intentToDeviceInfoDemo(getActivity(), info.getName());
                break;
            case 4:
                DemoIntent.intentToLocationDemo(getActivity(), info.getName());
                break;
            case 5:
                DemoIntent.intentToAlertDemo(getActivity(), info.getName());
                break;
            case 6:
                DemoIntent.intentToProtocolDemo(getActivity(), info.getName());
                break;
            case 7:
                DemoIntent.intentToAuthenticationDemo(getActivity(), info.getName());
                break;
            case 8:
                DemoIntent.intentToTvDemo(getActivity(), info.getName());
                break;
            case 9:
                DemoIntent.intentToSpeechCardDemo(getActivity(), info.getName());
                break;
            case 10:
                DemoIntent.intentToTTSInfoDemo(getMActivity(), info.getName());
                break;
            case 11:
                DemoIntent.intentToRenderAlertDemo(getMActivity(), info.getName());
                break;
            case 12:
                DemoIntent.intentToThirdPartDemo(getMActivity(), info.getName());
                break;
        }
    }

    @Override
    public void onConnected() {
        Toast.makeText(DemoApplication.getAppContext(), "onConnected", Toast.LENGTH_SHORT).show();
        ConsoleLogger.printDebugInfo(FunctionFragment.class, "onConnected");
        //SDK内部存在鉴权逻辑，如需要实现详情请见AuthenticationDemo
        DemoApp.getInstance().getCurreDuerDevice().setVerifySucc();
    }

    @Override
    public void onLocalConnected() {

    }

    @Override
    public void onConnectionFailed() {
        ConsoleLogger.printDebugInfo(FunctionFragment.class, "onConnectionFailed");
        Toast.makeText(DemoApplication.getAppContext(), "onConnectionFailed", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDisconnected() {
        ConsoleLogger.printDebugInfo(FunctionFragment.class, "onDisconnected");
        Toast.makeText(DemoApplication.getAppContext(), "onDisconnected", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        DemoApp.getInstance().getCurreDuerDevice().unregisterConnectionListener(this);
    }
}
